﻿myApp.factory('LoginFactory', function ($http, $q) {
    // login status
    var status = {};

    // set the status based on cookie
    if (localStorage.getItem('token')) {
        status.loggedin = true;
    } else {
        status.loggedin = false;
    }


    // Loggin 
    var Login = function () {
        var def = $q.defer;
        $http({
            method: 'POST',
            url: '/Token',
            data: 'username=' + user.username + '&password=' + user.password + '&grant_type=password',
            contentType: 'application/x-www-form-urlencoded'
        }).success(function (data) {
            console.log('data', data)
            localStorage.setItem('token', data.access_token);
            status.loggedin = true;
            def.resolve();
        }).error(function (data) {
            Logout();
            def.reject();
        })
        return def.promise;
    }

    // Logout
    var Logout = function () {
        localStorage.removeItem('token');
        status.loggedin = false;
    }


    // Get User Info
    var getInfo = function () {
        var def = $q.defer();

        return def.promise;
    }


});